﻿@using LeadMaster.Helpers
@using LeadMaster.Properties
@model LeadMaster.ViewModels.ViewModelClient

<style type="text/css">
.InfoRowHeight
{
    height: 20px;   
}
.sectionheader
{
    font-size: 9px;
    text-align: left;
 }
 .topsectiontitles
 {
    text-align: left;
    background-color: #BFFFFF;
 }
</style>

<div class="spacer"></div>

@if (@Model.FullListLeadResults != null && @Model.FullListLeadResults.Count() > 0)
{
<div class="spacer"></div>
<table style="width:3500px;">
<tr>
@for (int i = 0; i < @Model.Titles.Count(); i++)
{
    <th>
    <label style="color: #FF0000; font-size: 9px; font-family: Arial, Helvetica, sans-serif; font-weight: bold;">
    @if (Model.FullListLeadResults[i].LeadResult.LeadGroupQuestionsID != null && Model.FullListLeadResults[i].LeadResult.LeadGroupQuestionsID > 0)
    {
        if (Model.FullListLeadResults[i].LeadResult.LeadGroupQuestion.ClientNote!=null)
        {
            @Model.FullListLeadResults[i].LeadResult.LeadGroupQuestion.ClientNote
        }
    }
    </label>
    </th>    
}
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
</tr>

<tr>
@{
int colSpan = 1;
string blockTitle = "";
for (int i = 0; i < @Model.Titles.Count(); i++)
{
    if (Model.FullListLeadResults[i].LeadResult.LeadStatQuestionsID != null && Model.FullListLeadResults[i].LeadResult.LeadStatQuestionsID > 0)
    {
        if (blockTitle == Model.FullListLeadResults[i].LeadResult.LeadStatQuestion.LeadStat.Title)
        {
            colSpan++;
        }
        else
        {
            if (blockTitle.Trim().Length > 0)
            {
                <th colspan="@(colSpan)" class="topsectiontitles"><label class="sectionheader">@blockTitle</label></th>                    
            }
            colSpan = 1;
            blockTitle = Model.FullListLeadResults[i].LeadResult.LeadStatQuestion.LeadStat.Title;
        }
    }
    if (Model.FullListLeadResults[i].LeadResult.LeadGroupQuestionsID != null && Model.FullListLeadResults[i].LeadResult.LeadGroupQuestionsID > 0)
    {
        if (blockTitle == Model.FullListLeadResults[i].LeadResult.LeadGroupQuestion.LeadGroups.Title)
        {
            colSpan++;
        }
        else
        {
            if (blockTitle.Trim().Length > 0)
            {
                <th colspan="@(colSpan)" class="topsectiontitles"><label class="sectionheader">@blockTitle</label></th>                
            }
            colSpan = 1;
            blockTitle = Model.FullListLeadResults[i].LeadResult.LeadGroupQuestion.LeadGroups.Title;
        }
    }
    if (Model.FullListLeadResults[i].LeadResult.CompanyRegisteredID != null && Model.FullListLeadResults[i].LeadResult.CompanyRegisteredID > 0)
    {
        if (blockTitle == "Registration")
        {
            colSpan++;
        }
        else
        {
            if (blockTitle.Trim().Length > 0)
            {
                <th colspan="@(colSpan)" class="topsectiontitles"><label class="sectionheader">@blockTitle</label></th>
            }
            colSpan = 1;
            blockTitle = "Registration";
        }
    }
    if (Model.FullListLeadResults[i].LeadResult.CompanyRestrictionsID != null && Model.FullListLeadResults[i].LeadResult.CompanyRestrictionsID > 0)
    {
        if (blockTitle == "Restriction")
        {
            colSpan++;
        }
        else
        {
            if (blockTitle.Trim().Length > 0)
            {
            <th colspan="@(colSpan)" class="topsectiontitles"><label class="sectionheader">@blockTitle</label></th>
            }
            colSpan = 1;
            blockTitle = "Restriction";
        }
    }
    if (Model.FullListLeadResults[i].LeadResult.CompanySundriesID != null && Model.FullListLeadResults[i].LeadResult.CompanySundriesID > 0)
    {
        if (blockTitle == "Sundries")
        {
            colSpan++;
        }
        else
        {
            if (blockTitle.Trim().Length > 0)
            {
            <th colspan="@(colSpan)" class="topsectiontitles"><label class="sectionheader">@blockTitle</label></th>
            }
            colSpan = 1;
            blockTitle = "Sundries";
        }
    }
} 
<th colspan="@(colSpan)" class="topsectiontitles"><label class="sectionheader">@blockTitle</label></th>
}
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
</tr>
<tr>
@for (int i = 0; i < @Model.Titles.Count(); i++)
{
    <th style="background-color: #BFFFFF;">
    <label style="color: #FF0000;">
    @if (Model.FullListLeadResults[i].LeadResult.LeadStatQuestionsID != null && Model.FullListLeadResults[i].LeadResult.LeadStatQuestionsID > 0)
    {
        if (Model.FullListLeadResults[i].LeadResult.LeadStatQuestion.Mandatory == true)
        {
            @:M
        }
        else
        {
            @:I
        }
    }
    @if (Model.FullListLeadResults[i].LeadResult.LeadGroupQuestionsID != null && Model.FullListLeadResults[i].LeadResult.LeadGroupQuestionsID > 0)
    {
        if (Model.FullListLeadResults[i].LeadResult.LeadGroupQuestion.Mandatory == true)
        {
            @:M
        }
        else
        {
            @:I
        }
    }
    @if (Model.FullListLeadResults[i].LeadResult.CompanyRegisteredID != null && Model.FullListLeadResults[i].LeadResult.CompanyRegisteredID > 0)
    {
        if (Model.FullListLeadResults[i].LeadResult.CompanyRegistered.MandatoryLeadQuestion == true)
        {
            @:M
        }
        else
        {
            @:I
        }
    }
    @if (Model.FullListLeadResults[i].LeadResult.CompanyRestrictionsID != null && Model.FullListLeadResults[i].LeadResult.CompanyRestrictionsID > 0)
    {
        if (Model.FullListLeadResults[i].LeadResult.CompanyRestriction.MandatoryLeadQuestion == true)
        {
            @:M
        }
        else
        {
            @:I
        }
    }    
    @if (Model.FullListLeadResults[i].LeadResult.CompanySundriesID != null && Model.FullListLeadResults[i].LeadResult.CompanySundriesID > 0)
    {
        if (Model.FullListLeadResults[i].LeadResult.CompanySundry.MandatoryLeadQuestion == true)
        {
            @:M
        }
        else
        {
            @:I
        }
    }    
    </label>
    </th>    
}
<th style="background-color: #BFBFBF;">&nbsp;</th>
<th style="background-color: #BFBFBF;">&nbsp;</th>
<th style="background-color: #BFBFBF;">&nbsp;</th>
<th style="background-color: #BFBFBF;">&nbsp;</th>
</tr>
<tr>
@for (int i = 0; i < @Model.Titles.Count(); i++)
{
    <th style="border: 1px solid #000000; background-color: #999999;">
    <label style="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 10px;">
    @Model.Titles[i]
    </label>
    </th>    
}
<th style="border: 1px solid #000000; background-color: #BFBFBF;">
<label style="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 10px;">
Call Centre
</label>
</th>
<th style="border: 1px solid #000000; background-color: #BFBFBF;">
<label style="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 10px;">
Purchase Order
</label>
</th>
<th style="border: 1px solid #000000; background-color: #BFBFBF;">
<label style="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 10px;">
New or Replacement  
</label>
</th>
<th style="border: 1px solid #000000; background-color: #BFBFBF;">
<label style="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 10px;">
Days Left To Return
</label>
</th>
<th style="border: 1px solid #000000; background-color: #BFBFBF;">
<label style="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 10px;">
Current Status
</label>
</th>
<th style="border: 1px solid #000000; background-color: #BFBFBF;">
<label style="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 10px;">
Return Reason
</label>
</th>
<th style="border: 1px solid #000000; background-color: #BFBFBF; width: 200px; !important">
<label style="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 10px;">
Notes
</label>
</th>
</tr>
<tr>
@{
int iCounter = 0;
int daysDiff = 0;
string poID=string.Empty;
foreach (var item in Model.FullListLeadResults)
{
    <td style="border: 1px solid #000000;">
    <label style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold;">
    @item.LeadResult.Answer
    </label>
    </td>
    iCounter++;
    if (iCounter == item.LeadResult.QuestionBlock)
    {
        daysDiff = 5 - (Convert.ToInt16((DateTime.Now - (Convert.ToDateTime(item.LeadStatus.LeadDate))).TotalDays));
        daysDiff += AdditionalDays.AddLeadDays(item.LeadResult.LeadDate.DayOfWeek);
        daysDiff = daysDiff < 0 ? 0 : daysDiff;
        poID = item.LeadStatus.PurchaseOrderID.ToString();
        <text>
            <td style="border: 1px solid #000000; vertical-align: middle;">
                <label>@item.LeadStatus.CallCentre.CallCentre</label>
            </td>
            <td style="border: 1px solid #000000; vertical-align: middle;">
            <input type="hidden" name="LeadDate" value="@(item.LeadStatus.LeadDate)" />
            <select class="allocatepid@(ViewBag.Indexer)" data-id="@(item.LeadStatus.LeadStatusID)" data-val="true" data-val-number="The field PurchaseOrderID must be a number." id="LeadStatus_PurchaseOrderID" name="LeadStatus.PurchaseOrderID">
            @if (item.LeadStatus.PurchaseOrderID==null)
            {
                <option value="0">Allocate PO</option>
            }
            @foreach (var poList in Model.PurchaseOrdersList)
            {
                <option value="@(poList.Value)"
                @if (poList.Value==item.LeadStatus.PurchaseOrderID.ToString())
            	{
		            @:selected
	            }
                >@poList.Text</option>
            }
            </select>
            </td>
            <td style="border: 1px solid #000000;">
            <select class="allocatereplacement@(ViewBag.Indexer)" data-id="@(item.LeadStatus.LeadStatusID)" data-val="true" data-val-number="The field PurchaseOrderID must be a number." id="LeadStatus_Replacement" name="LeadStatus.Replacement">
            <option value="0"
            @if (item.LeadStatus.Replacement==null)
            	{
		            @:selected
	            }
            >New</option>
            <option value="1"
            @if (item.LeadStatus.Replacement==true)
            	{
		            @:selected
	            }
            >Replacement</option>
            </select>
            </td>
            <td style="border: 1px solid #000000;">
            <label style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold;">
            @daysDiff
            </label>
            </td>
            <td style="border: 1px solid #000000;">
	            <label>@item.LeadFlag.Description</label>    
            </td>
            <td style="border: 1px solid #000000;">
            @if (item.LeadReason.LeadReasonsID>1)
            {
            <label style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold; ">
            @item.LeadReason.Description
            </label>
            }
            else
            {
             @:&nbsp;   
            }
            @if (item.QCReason!=null)
            {
            <label style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold; ">
            @item.QCReason
            </label>
            }
            else
            {
             @:&nbsp;   
            }
            </td>
            <td style="border: 1px solid #000000;">
            <textarea style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold; width: 150px; height: 75px;">@item.LeadStatus.Notes</textarea>
            </td>
            @if (@item.LeadStatus.VoxFile != null)
            {
			<td>
			<audio controls>
				<source src="@Url.Action("PlayVoxFile","Client")/?voxFile=@(Server.UrlEncode(item.LeadStatus.VoxFile))" type="audio/mp3" />
				<p>Your browser does not support HTML 5 audio element</p>
			</audio>
			</td>
            }
            <td>
             <img src="~/Images/delete.ico" style="width: 20px; height: 20px" class="deletelead@(ViewBag.Indexer)" data-id="@(item.LeadStatus.LeadStatusID)"/>
            </td>
        </text>
        @:</tr><tr>
        iCounter = 0;
    }
}
}
</tr>
</table>
}
else
{
<h3>No Leads have been entered yet.</h3>
}

<div id="changepid"></div>

<script type="text/javascript">
    $(document).ready(function () {
        // Leads/ReallocatePID/?leadStatusID=10&pid=10

        $(".allocatepid@(ViewBag.Indexer)").change(function () {
            $.ajax({
                type: "GET",
                url: "@(Url.Action("ReallocatePID","Leads"))/?leadStatusID=" + $(this).data("id") + "&pid=" + $(this).val(),
                success: successfunc()
            });
        });

        function successfunc() {
        alert("allocated");
        }

        $(".allocatereplacement@(ViewBag.Indexer)").change(function () {
            $.ajax({
                type: "GET",
                url: "@(Url.Action("ReallocateReplacement","Leads"))/?leadStatusID=" + $(this).data("id") + "&replacement=" + $(this).val(),
                success: successfunc()
            });
        });

        $(".deletelead@(ViewBag.Indexer)").click(function() {
        var ans = confirm("Are you sure you wish to delete this lead ?")
        if (ans == true) {
            var contactURL = '@(Url.Action("DeleteLead", "Leads"))?id=' + $(this).data("id");
            window.location.href = contactURL;
        }
        });
    });
</script>

